package com.xiaohui.gwc.service.Impl;

import com.baomidou.mybatisplus.core.assist.ISqlRunner;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xiaohui.gwc.mapper.ProductMapper;
import com.xiaohui.gwc.mapper.RdMapper;
import com.xiaohui.gwc.mode.Product;
import com.xiaohui.gwc.mode.Rd;
import com.xiaohui.gwc.mode.Result;
import com.xiaohui.gwc.mode.ReturnCode;
import com.xiaohui.gwc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: Poppin
 * @Date: 2021/11/30 12:40
 * @Version: 1.0
 */
@Service
public class ProductServiceImpl implements ProductService {


    private ProductMapper productMapper;
    private RdMapper rdMapper;

    @Autowired
    private ProductServiceImpl(ProductMapper productMapper,RdMapper rdMapper){
        this.productMapper = productMapper;
        this.rdMapper = rdMapper;
    }

    /**
     * 查询商品详情
     * @param pid
     * @return
     */
    @Override
    public Result product(int pid) {

        Product product = productMapper.selectById(pid);


        Rdwrite(pid);
        return new Result<Product>(
                ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getType(), "成功",product
        );
    }

    public void Rdwrite(int pid){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("prodid",pid);
        int count = rdMapper.selectCount(queryWrapper);
        if (count==0){
            Rd rd = new Rd();
            rd.setProdid(pid);
            rd.setRdsl(1);
            rdMapper.insert(rd);
        }else {
            Rd rd = new Rd();
            rd = rdMapper.selectById(pid);
            UpdateWrapper<Rd> rdUpdateWrapper = new UpdateWrapper<>();
            rdUpdateWrapper.eq("prodid",pid)
                    .set("rdsl",rd.getRdsl()+1);
            rdMapper.update(null,rdUpdateWrapper);
        }
    }
}